package com.tc.mq;

import com.rabbitmq.client.Channel;
import com.tc.client.UserServiceClient;
import com.tc.controller.WebSocketServer;
import com.tc.dao.*;

import com.tc.pojo.Address;
import com.tc.pojo.Commodity;
import com.tc.pojo.Order;
import com.tc.pojo.Trucks;
import com.tc.vo.OrderAndAddress;
import com.tc.vo.RedisOrderwait;
import com.tc.vo.UserVo;
import entity.Result;
import entity.StatusCode;
import io.swagger.models.auth.In;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Component
public class OrderConsumer {

    @Autowired
    private TrucksMapper trucksMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper itemMapper;

    @Autowired
    private BusDetailsMapper busDetailsMapper;


    @Autowired
    private CommodityMapper commodityMapper;


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private WebSocketServer webSocketServer;
    @RabbitListener(queues = "orderwait-queue")
    @RabbitHandler
    public void handleOrderQueue(@Payload Map map, Channel channel, @Headers Map headers){
        RedisOrderwait RedisOrder=new RedisOrderwait();
        Order order= (Order) map.get("order");
        Address address= (Address) map.get("address");
        Commodity commodity=commodityMapper.findCommodityById(order.getCid());
        RedisOrder.setCdyname(commodity.getCdyname());
        Trucks trucks=trucksMapper.findTrucksbyId(order.getTid());
        RedisOrder.setTname(trucks.getCdyname());
        //时间格式存储
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm:ss");
        String time=simpleDateFormat.format(order.getCreateTime());
        RedisOrder.setCreateTime(time);
        //订单号存储
        RedisOrder.setOrderSn(order.getOrderSn());
        UserVo user=userServiceClient.getUser(order.getUid().intValue());
        RedisOrder.setUsername(user.getName());
        RedisOrder.setAddress(address);
        RedisOrder.setOrder(order);
        RedisOrder.setHeadImg(user.getHeadImg());
        redisTemplate.opsForHash().put("orderwait",order.getOrderSn(),RedisOrder);
        try {
            WebSocketServer.sendInfo(new Result(true,1000, "您有一条新的订单"+order.getOrderSn()),"1");
        } catch (IOException e) {
            e.printStackTrace();
        }
//        redisTemplate.opsForList().rightPush("orderwait",RedisOrder);
        Long id = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //签收数据
        try {
            channel.basicAck(id,false);
        } catch (IOException e) {
            e.printStackTrace();
    }
//        //创建订单数据
//        Order order = new Order();
//        order.setOrderSn(orderNo);
//        order.setCreateTime(new Date());
//        order.setMemberId(userId);
//        order.setMemberUsername("hello");
//        order.setReceiverName("");
//        order.setReceiverPhone("");
//        orderMapper.insertSelective(order);
//        //订单明细
//        OrderItem item = new OrderItem();
//        item.setOrderSn(orderNo);
//        item.setProductName(product.getTitle());
//        item.setProductId(productId);
//        item.setProductPic(product.getPic());
//        item.setOrderId(order.getId());
//        //更新库存数量
//        item.setProductSn(product.getProductSn());
//        itemMapper.insertSelective(item);
//        seckillMapper.updateCountById(psId,-1);
        //同时将待支付的订单数据，存储到新的队列中，等待死信队列处理。
//        rabbitTemplate.convertAndSend("pay-exchange","pay."+orderNo,data);
    }
}
